﻿using ENNote.Common;
using ENNote.Dto;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;

namespace ENNote.Controllers
{
    /// <summary>
    /// 笔记本控制器
    /// </summary>
    public class NotebookController : Controller
    {

        /// <summary>
        /// 所有笔记本
        /// </summary>
        /// <returns></returns>
        [HttpGet("/notebook/all")]
        public ResData GetAll()
        {
            var notebooks = new List<string>();
            var dirInfo = new DirectoryInfo(MyConfig.localData);

            foreach (var dir in dirInfo.GetDirectories())
            {
                notebooks.Add(dir.Name);
            }
            return new ResData(200, "获取成功", notebooks);
        }


        /// <summary>
        /// 添加笔记本
        /// </summary>
        /// <param name="name">新笔记本名称</param>
        /// <returns></returns>
        [HttpPost("/notebook/add")]
        public ResData CreateNotebook([FromBody] AddNotebookDto addNotebookDto)
        {
            if (string.IsNullOrEmpty(addNotebookDto.Name))
            {
                return new ResData(400, "请输入新笔记本名称");
            }

            try
            {
                Directory.CreateDirectory(Path.Combine(MyConfig.localData, addNotebookDto.Name));
                return new ResData(200, "创建成功");
            }
            catch (Exception e)
            {
                return new ResData(500, "创建失败：" + e.Message);
            }
        }


        /// <summary>
        /// 获取当前笔记本下的全部笔记
        /// </summary>
        /// <param name="currentNotebook">当前笔记本</param>
        /// <returns></returns>
        [HttpGet("/notebook/allNote")]
        public ResData GetAllNote([FromQuery] string currentNotebook)
        {
            if (string.IsNullOrEmpty(currentNotebook))
            {
                return new ResData(400, "请选择笔记本");
            }

            var notes = new List<string>();

            var notebookDir = Path.Combine(MyConfig.localData, currentNotebook);
            try
            {
                var notebookDirInfo = new DirectoryInfo(notebookDir);

                foreach (var file in notebookDirInfo.GetFiles())
                {
                    notes.Add(file.Name.Replace(".md", ""));
                }
                return new ResData(200, "获取成功", notes);
            }
            catch (Exception e)
            {
                return new ResData(500, "获取失败" + e.Message);
            }

        }


        /// <summary>
        /// 删除笔记本
        /// </summary>
        /// <param name="currentNotebook"></param>
        /// <returns></returns>
        [HttpDelete("/notebook/remove")]
        public ResData RemoveNote(string currentNotebook)
        {
            if (string.IsNullOrEmpty(currentNotebook))
            {
                return new ResData(400, "请选中笔记本");
            }

            var path = Path.Combine(MyConfig.localData, currentNotebook);
            try
            {
                // 删除文件夹
                Directory.Delete(path, true);
                return new ResData(200, "删除成功");
            }
            catch (Exception e)
            {
                return new ResData(500, "删除失败：" + e.Message);
            }
        }
    }
}
